#!/usr/bin/env python
#
# Copyright 2008 Richard J. Bell. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

""" Wiki classes for geo wiki (great-loop) site. """

# Python Imports
import urllib

# Google App Engine Imports
from google.appengine.api import users
from google.appengine.ext import webapp

# Application Imports
from app.baserequesthandler import BaseRequestHandler
from app.wikipage import WikiPage
from app.userhandlers import UserSignIn
from model import WikiUser

class List(BaseRequestHandler):
    """ List extends the BaseRequestHandler, and handles all
      requests to the url http://great-loop.appspot.com/wikilist
    """
    def get(self):
        """ wikilist get request handler """
        wiki_list = WikiPage.listall()     # get list of all wiki's
        self.generate('wikilist.html',     # build template values
                      template_values={
                          'page_title': 'Wiki List',
                          'number_wikis': len(wiki_list),
                          'wiki_list': wiki_list}) 

class GetPage( webapp.RequestHandler ):
    """ Handle all page get requests, /wikiget/* """
    def get(self, page_title):
        """ wikiview get request handler """
        # Get the data
        page = WikiPage.load( page_title )
        # Wikify the wiki body
        wikified = WikiPage.wikify( page.content )
        # Can NOT use JSONEncoder since the page object contains MUCH beside 
        # our data.  Instead use a template to build our response object.
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write( wikified )

class View(BaseRequestHandler):
    """ Handle all page view requests, /wikiview/* """
    def get(self, page_title):
        """ wikiview get request handler """
        # Get the data
        page = WikiPage.load( page_title )
        # Wikify the wiki body
        wikified = WikiPage.wikify( page.content )
        # Render the template wikiview.html, which extends base.html
        self.generate('wikiview.html', 
                      template_values = {
                          'page': page,
                          'wikified': wikified,
                          'author': page.author.user.nickname(),
                          'email': urllib.quote(page.author.user.email())
                         }
                      )
        
class Edit(BaseRequestHandler):
    """ Handles all requests to /wikiedit/* """
    def get(self, page_title):
        """ wikiedit get request handler """
        UserSignIn.require_signin( self, '/wikiedit/' + page_title )
        page = WikiPage.load( page_title )
        self.generate('wikiedit.html', template_values={'page': page })

class Save(BaseRequestHandler):
    """ Save extends the BaseRequestHandler, and handles all
      requests to the url http://great-loop.appspot.com/wikisave/*
    """
    def post(self, page_title):
        """ wikisave post request handler """
        UserSignIn.require_signin( self, '/wikiedit/' + page_title )
        WikiPage.save( page_title, 
                       self.request.get('title'), 
                       self.request.get('body')
                      )
        # After the entry has been saved, direct the user back to view the page
        self.redirect('/wikiview/' + page_title)
